import Vue from 'vue'
import Router from 'vue-router'

import login from '@/views/login'

import Container from '@/container/Container'
import Dashboard from '@/views/dashboard'

import User from '@/views/user'

import Article from '@/views/article'

Vue.use(Router)

const router = new Router({
  //mode: 'history',  //去掉url中的#
  routes: [
    {
      path: '/',
      redirect: '/dashboard',
      name: 'Container',
      component: Container,
      children: [
        {path: 'dashboard', name: '首页', component: Dashboard, },
        {path: 'user', name: '用户列表', component: User,},
        {path: 'article', name: '文章列表', component: Article,},
      ]
    },
    // {
    //   path: '/',
    //   name: 'home',  // 路由名称
    //   component: home  // 组件对象
    // },
    {
      path: '/login',
      name: 'login',
      component: login
    }
  ]
})

// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    let token = localStorage.getItem('token')
    if (token === null || token === '') {
      next('/login')
    } else {
      next()
    }
  }
})

export default router
